Watch Points is a MacsBug feature by which memory locations can be tracked for modification during program execution. It uses the page write protection feature of the processor to track all writes to a page that the watch location lies within. MacsBug handles and filters the protection faults that occur during writes to the page, and either lets the write complete normally, or breaks into the debugger if the write hits within the watch range.
The original Watch Points mechanism was introduced with MacsBug version 6.5.4a1. It was based upon the 68K processor's bus error exception mechanism, and suffered from a number of deficiencies, most of which added up to make it a very difficult piece of code to maintain. Because of the complexity of the implementation, and the execution level at which it worked (or didn't, as was usually the case), it proved to be not very reliable. Some people used it to successfully track down bugs, but a majority of others reported that it usually locked up their machines. As a debugging tool, this isn't acceptable. Thus, along came the New Watch Points implementation, thanks to a suggestion and the initial prototype code by Mike Neil (formerly of Apple and Connectix).
THE NEW IMPLEMENTATION
The current implementation is much more robust, primarily because it lives at a relatively low level in the Mac OS execution environment. Whereas the original version was based around the 68K exception model, and thus had to be bound by limitations imposed by the 68K Emulator (primarily 68K supervisor mode vs. user mode and the breadth of 68K exception vectors), the new code functionally lives below the Emulator by adopting some limited knowledge of the PowerPC NanoKernel.
This version offers much higher performance by pushing most of the logic for handling the protection exceptions down into small bits of PowerPC assembly code (one instance lives under the Emulator, and one lives under all PowerPC-native code). Each handler has a single entry point, and more or less atomically handles write completion to ranges that don't interest it (those that hit in watch pages, but miss the watch range).
The exact details of the implementation aren't interesting to most, but what is interesting is that this version does not require VM to be enabled, and offers much more potential for watching accesses from all execution levels. With the new implementation you can detect writes that occur from any piece of code in the system or application, whether it's 68K or PowerPC. Well, almost. See the caveats below for current limitations.
PRESENT LIMITATIONS
There are a number of limitations in the current implementation. Some are bugs, and some are features that just aren't done being implemented yet. In this first version, this WP mechanism works best on blocks of data that live in heaps, and were allocated at some level by the Mac OS Memory Manager. What follows is a list of the various problems we know about so far:
- Due to a bug in the 68K Emulator, MacsBug requires an update extension to have patched the Emulator. This means you can't presently do Watch Points until INIT time. MacsBug will let you know if the current Emulator doesn't have the bug fixed.
- Due to the above bug, Watch Points only work on machines that have a 2.x DR Emulator. This includes all machines released since the 9500 (the 9500 is included).
- It almost works on the first page of memory ($00000000) where most of low memory lies, but we haven't yet figured out how to ignore the Process Manager writing to absolute zero ($00000000). Thus, you won't yet be able to watch for writes to NIL.
- You can't watch locations on the stack. If you do, you'll eventually bus error with the stack pointer off in hyperspace. We understand this, but don't yet have a deliverable fix (it's not MacsBug).
- You can't watch a location that is within a page that someone will be using atomic operations to (lwarx/stwcx). If you do, the reservation will always be lost, and the atomic operation will loop forever. If the machine seems to lock up, and you NMI in at the previous instruction is a “stwcx.”, you need to clear the Watch Point to make forward progress. This is due to a NanoKernel feature. It's not clear if this can be fixed without completely replacing the NanoKernel.
- Watch Point expressions don't work. That is, you can't yet say to watch a location and only break if an optional expression is true. This will be fixed in a release after 6.5.4a6.
- You can't hit a Watch Point and step away from it without clearing it. This is the same problem as the lack of expression support. This will also be fixed in a release after 6.5.4a6.
Apple, the Apple logo, Apple IIGS, Macintosh, MPW, Power Macintosh, and PowerBook are trademarks of Apple Computer, Inc., registered in the United States and other countries. PowerPC is a trademark of International Business Machines Corporation, used under license therefrom.